﻿Imports System.Data
Imports System.Data.SqlClient

Partial Class WebForm_ManageBanner_BannerUploader
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.FormView1.InsertItemTemplate = Me.FormView1.ItemTemplate
        Me.FormView1.EditItemTemplate = Me.FormView1.ItemTemplate
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(Me.FormView1)
    End Sub

    '上傳檔案
    Protected Sub btnUpLoad_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Me.UploadFile()
    End Sub

#Region "SaveToolBar存檔"
    Public Function SaveData() As Boolean

        If FormAccessMode = FormViewMode.Insert Then
            If Not UploadFile() Then
                Return False
            End If
        Else
            Dim hiFileUpload As FileUpload = TryCast(FormView1.FindControl("hiFileUpload"), FileUpload)
            If hiFileUpload.HasFile AndAlso Not Me.UploadFile() Then
                Return False
            End If
        End If

        Dim strFileLongName As String = String.Empty
        Dim strFileName As String = String.Empty
        Dim strFileType As String = String.Empty
        Dim strFileShortName As String = String.Empty

        If PostedFileName <> String.Empty Then
            strFileLongName = PostedFileName
            strFileName = strFileLongName.Substring(strFileLongName.LastIndexOf("\") + 1)
            strFileType = strFileName.Substring(strFileName.LastIndexOf(".") + 1)
            strFileShortName = strFileName.Substring(0, strFileName.LastIndexOf("."))
        End If

        '將檔案上傳至資料庫
        Dim dbUtil As MyclsDBUtility = MyclsDBUtility.GetInstance()

        If FormAccessMode = FormViewMode.Insert Then

            dbUtil.Banner_Insert(FileClass, FileType, strFileName, strFileShortName, strFileType, FileSavePath, FileSaveName, TryCast(FormView1.FindControl("txtDesc"), TextBox).Text, Page.User.Identity.Name, TryCast(FormView1.FindControl("txtUrl"), TextBox).Text, TryCast(FormView1.FindControl("ddlActive"), DropDownList).SelectedValue)
        Else
            dbUtil.Banner_Update(strFileName, strFileShortName, strFileType, FileSavePath, FileSaveName, TryCast(FormView1.FindControl("txtDesc"), TextBox).Text, Page.User.Identity.Name, TryCast(FormView1.FindControl("txtUrl"), TextBox).Text, TryCast(FormView1.FindControl("ddlActive"), DropDownList).SelectedValue, BannerID)
        End If

        Return True
    End Function
#End Region

    Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound

        If FormAccessMode = FormViewMode.Insert Then

            TryCast(FormView1.FindControl("Image1"), Image).Visible = False
            TryCast(FormView1.FindControl("txtUrl"), TextBox).Text = String.Empty
            TryCast(FormView1.FindControl("txtDesc"), TextBox).Text = String.Empty

        Else

            FileSaveName = DataBinder.Eval(Me.FormView1.DataItem, "FileSaveName")

        End If
        'TryCast(FormView1.FindControl("chk_hiFileUpload"), RequiredFieldValidator).Enabled = (FormAccessMode = FormViewMode.Insert)
        TryCast(FormView1.FindControl("chk_txtPostedFileName"), RequiredFieldValidator).Enabled = (FormAccessMode = FormViewMode.Insert)

    End Sub

    Protected Sub ddlActive_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim ddlActive As DropDownList = sender

        If ddlActive.Items.Count = 0 Then

            For Each DR As DataRow In MyObj.GetCodeMappingData("Banner", "Active").Rows
                ddlActive.Items.Add(New ListItem(DR.Item("Text"), DR.Item("Value")))
            Next

        End If
    End Sub

    Protected Sub FormView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.PreRender
        ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(FormView1.FindControl("btnUpLoad"))
    End Sub

End Class

Partial Class WebForm_ManageBanner_BannerUploader
    '上傳檔案
    Private Function UploadFile() As Boolean

        Dim hiFileUpload As FileUpload = TryCast(FormView1.FindControl("hiFileUpload"), FileUpload)

        If Not hiFileUpload.HasFile Then
            MyObj.ShowMessage(Me.GetLocalResourceObject("FileFail"))
            Return False
        End If

        If hiFileUpload.PostedFile.ContentType.IndexOf("image") = -1 Then
            MyObj.ShowMessage(Me.GetLocalResourceObject("FileFormatFail"))
            Return False
        End If

        If Not String.IsNullOrEmpty(FileSavePath) Then
            'Dim strFileSaveLongName As String = String.Format("{0}{1}", Server.MapPath(FileSavePath), FileSaveName)
            FileType = hiFileUpload.PostedFile.FileName.Substring(hiFileUpload.PostedFile.FileName.LastIndexOf(".") + 1)
            FileSaveName = Guid.NewGuid()
            Dim strFileSaveLongName As String = String.Format("{0}{1}.{2}", Server.MapPath(FileSavePath), FileSaveName, FileType)

            '判斷圖檔寬度是否過大
            Dim img As Drawing.Image = Drawing.Image.FromStream(hiFileUpload.PostedFile.InputStream)
            If img.Width > 175 Then
                MyObj.ShowMessage(Me.GetLocalResourceObject("ImageWidthFail"))
                Return False
            End If
            Try

                '將檔案上傳至server 
                hiFileUpload.PostedFile.SaveAs(strFileSaveLongName)

                PostedFileName = hiFileUpload.PostedFile.FileName

                'Dim Image1 As Image = FormView1.FindControl("Image1")

                'Image1.ImageUrl = FileSavePath + FileSaveName.ToString() + "." + FileType
                'Image1.Visible = True
                'MyObj.ShowMessage(Image1.ImageUrl)
            Catch ex As Exception
                'If System.IO.File.Exists(strFileSaveLongName) Then
                'System.IO.File.Delete(strFileSaveLongName)
                'End If
                MyObj.ShowMessage(Me.GetLocalResourceObject("FileUpLoadFail"))
                MyObj.WriteLog(ex.ToString())
                Return False
            End Try

        End If
        Return True
    End Function

    Public Sub InitData()

        If BannerID = 0 Then
            FormAccessMode = FormViewMode.Insert
        Else
            FormAccessMode = FormViewMode.Edit
        End If

        If FormAccessMode = FormViewMode.Insert Then

            FormView1.ChangeMode(FormViewMode.Insert)
            FileSaveName = Guid.NewGuid()
            FormView1.DataBind()
        Else

            FormView1.ChangeMode(FormViewMode.Edit)
            ObjectDataSource1.SelectParameters("BannerID").DefaultValue = BannerID
            FormView1.DataSourceID = ObjectDataSource1.ID
            FormView1.DataBind()

        End If

        PostedFileName = String.Empty

    End Sub

    Public Property FormAccessMode() As FormViewMode
        Get
            Return ViewState("FormAccessMode")
        End Get
        Set(ByVal value As FormViewMode)
            ViewState("FormAccessMode") = value
        End Set
    End Property

    Public Property FileClass() As String
        Get
            Return ViewState("FileClass")
        End Get
        Set(ByVal value As String)
            ViewState("FileClass") = value
        End Set
    End Property

    Public Property FileType() As String
        Get
            Return IIf(String.IsNullOrEmpty(ViewState("FileType")), String.Empty, ViewState("FileType"))
        End Get
        Set(ByVal value As String)
            ViewState("FileType") = value
        End Set
    End Property

    Public Property BannerID() As Integer
        Get
            Return ViewState("BannerID")
        End Get
        Set(ByVal value As Integer)
            ViewState("BannerID") = value
        End Set
    End Property

    Public Property PostedFileName() As String
        Get

            Return TryCast(FormView1.FindControl("txtPostedFileName"), TextBox).Text

        End Get
        Set(ByVal value As String)
            TryCast(FormView1.FindControl("txtPostedFileName"), TextBox).Text = value
        End Set
    End Property

    Public Property FileSavePath() As String
        Get
            Return ViewState("FileSavePath")
        End Get
        Set(ByVal value As String)
            ViewState("FileSavePath") = value
        End Set
    End Property

    Public Property FileSaveName() As Guid

        Get
            Return ViewState("FileSaveName")
        End Get
        Set(ByVal value As Guid)
            ViewState("FileSaveName") = value
        End Set

    End Property

End Class





